<%@page import="org.swimv2.interfaces.FriendshipManagerRemote"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="org.swimv2.entities.*" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="org.swimv2.utils.ContextUtil" %>
<%@ page import="org.swimv2.interfaces.FriendshipManagerRemote" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
User          curr_u = (User) request.getSession().getAttribute("user");
@SuppressWarnings("unchecked")
List<Skill>   skills = (List<Skill>) request.getAttribute("skills");
@SuppressWarnings("unchecked")
List<User> users_res = (List<User>) request.getAttribute("users_result");
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
@import url(/WEBswim2/css/main.css);
</style>
<title>Search Users</title>
</head>
<body>

<p class="homenav"><a href="/WEBswim2/home.jsp"><span>Home</span></a></p>
<ul class="ul_nav">
<% if (curr_u != null) { %>
    <li><a href="/WEBswim2/Logout">Logout</a></li>
    <li><a href="/WEBswim2/user/profile.jsp">Il Mio Profilo</a> <em>[<% out.print(curr_u.getFirstName() +" "+ curr_u.getLastName()); %>]</em></li>
<% } %>
</ul>

<h1>Ricerca Utenti</h1>
<%
    String msg = (String) request.getAttribute("message");
    if (msg != null) {
        out.println("<p class=\"message\">"+ msg +"</p>");
    }
    String err = (String) request.getAttribute("error_message");
    if (err != null) {
        out.println("<p class=\"error_message\">"+ err +"</p>");
    }
%>

<%
    if (users_res != null && curr_u == null) {
    	out.println("<p class=\"message\">Per chiedere un aiuto lavorativo devi registrarti.</p>");
    }

%>


<h2>Ricerca per nome</h2>
<form action="/WEBswim2/SearchUsers" method="post">
  <fieldset>
    <input type="hidden" name="how" value="NAME" />
    <label for="name">Inserisci nome:</label><br/>
    <input type="text" name="name"/>
    <input type="submit" name="submit" value="Cerca"/>
  </fieldset>
</form>

<h2>Ricerca per competenze</h2>
<form action="/WEBswim2/SearchUsers" method="post">
  <fieldset>
    <input type="hidden" name="how" value="SKILL" />
    <label for="skill">Competenza richiesta:</label><br/>
    <select name="skill">
    <%
       for (Skill s : skills) {
    	   out.println("<option value='"+ s.getId() +"'>"+ s.getName() +"</option>");
       }
     %>
    </select>
    <input type="submit" name="submit" value="Cerca"/>
  </fieldset>
</form>

<%
if (users_res != null) {
	
	/* Ricerca di un utente loggato */
	if (curr_u != null) {
		Context ctx = ContextUtil.getInitialContext();
		FriendshipManagerRemote mgr_f =
				(FriendshipManagerRemote) ctx.lookup("FriendshipManagerJNDI");
		
		out.println("<h2>Amici trovati:</h2>");
		out.println("<ul>");
		for (User u : users_res) {
			if (!u.equals(curr_u) && mgr_f.isFriendOf(curr_u, u))
				out.println("<li>"
						   + "<a href='/WEBswim2/user/show.jsp?user_id="
			               + Integer.toString(u.getId()) +"'>"
						   + u.getFirstName() + " " + u.getLastName()
						   +"</a></li>");
		}
		out.println("</ul>");
		
		out.println("<h2>Altri utenti trovati:</h2>");
		out.println("<ul>");
        for (User u : users_res) {
            if (!u.equals(curr_u) && !mgr_f.isFriendOf(curr_u, u))
                out.println("<li>"
                           + "<a href='/WEBswim2/user/show.jsp?user_id="
                           + Integer.toString(u.getId()) +"'>"
                           + u.getFirstName() + " " + u.getLastName()
                           +"</a></li>");
        }
        out.println("</ul>");
	}
	
	/* Ricerca di un utente non loggato */
	else {
	    out.println("<h2>Utenti trovati:</h2>");
	    out.println("<ul>");
	    for (User u : users_res) {
	    	out.println("<li>"
                       + "<a href='/WEBswim2/user/show.jsp?user_id="
                       + Integer.toString(u.getId()) +"'>"
                       + u.getFirstName() + " " + u.getLastName()
                       +"</a></li>");
	    }
	    out.println("</ul>");
	}
}
%>

</body>
</html>